//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：HDMHotFixInspector
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace HDMCode.Hotfix
{
#if UNITY_EDITOR
    [CustomEditor(typeof(HDMAssetsHotfix))]
    public class HDMHotFixInspector : DrawEvent
    {
        public virtual void OnEnable()
        {
            InitDrawList();
            InitDrawEvent();
        }

        public override void OnInspectorGUI()
        {
            HDMAssetsHotfix script = (HDMAssetsHotfix)target;
            DrawTile();
            StarDrawFields(script);
            UpdateProperty(() =>
            {
                UpdateDrawList();
                GUILayout.Space(5);
                UpdateDrawEvent();
                DrawAsnyButton(script);
                GUILayout.Space(5);
            });
            serializedObject.ApplyModifiedProperties();
            if (GUI.changed) EditorUtility.SetDirty(target);
        }

        /// <summary>
        /// 更新属性
        /// </summary>
        /// <param name="action"></param>
        public void UpdateProperty(Action action)
        {
            serializedObject.Update();
            action?.Invoke();
            serializedObject.ApplyModifiedProperties();
        }

        /// <summary>
        /// 绘制标题
        /// </summary>
        public void DrawTile()
        {
            GUI.color = new Color(255, 129, 203);
            GUILayout.Space(10);
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));
            GUILayout.Box("热更新下载管理-海盗猫", new[] { GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true) });
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));
            GUI.color = Color.white;
            EditorGUILayout.Space();
        }

        /// <summary>
        /// 同步程序集
        /// </summary>
        /// <param name="script">主脚本</param>
        public void DrawAsnyButton(HDMAssetsHotfix script)
        {
            if (GUILayout.Button("一键同步程序集"))
            {
                var files = HDMCode.FrameEditor.HDMEditorFileIO.GetSyncAllFile(DrawMeun.drawUISetting.ScriptOutPutPath + "/MateDLL");
                foreach (var item in files)
                {
                    string fileme = Path.ChangeExtension(item.fileName, null);
                    script.mateDate.Add(fileme);
                }
            }
        }


    }
#endif
}